package com.itboye.mengwu.app.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.itboye.config.GlobalConfig;
import com.itboye.core.utils.XImageLoader;
import com.itboye.core.utils.XPicasso;
import com.itboye.inageview.ImagePagerActivity;
import com.itboye.mengwu.R;
import com.itboye.mengwu.data.model.AllData;
import com.itboye.mengwu.data.model.MyBoyBean;
import com.itboye.mengwu.data.model.TieZiBaoHanReply;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap.Config;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

@SuppressLint("SimpleDateFormat")
public class MyBoyAdapter extends BaseAdapter {

	ArrayList<TieZiBaoHanReply.GetAList> list;
	Context context;
	OnClickListener deleteLisenter;

	public MyBoyAdapter(ArrayList<TieZiBaoHanReply.GetAList> list,
			Context context, OnClickListener deleteLisenter) {
		this.list = list;
		this.context = context;
		this.deleteLisenter = deleteLisenter;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int postion) {
		// TODO Auto-generated method stub
		return postion;
	}

	@Override
	public long getItemId(int postion) {
		// TODO Auto-generated method stub
		return postion;
	}

	@SuppressWarnings("unused")
	@Override
	public View getView(int postion, View contview, ViewGroup arg2) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		final List<String> imageUrls = list.get(postion).getImg();
		if (holder == null) {
			holder = new ViewHolder();
			if (imageUrls.size() == 3) {
				contview = LayoutInflater.from(context).inflate(
						R.layout.mybabyfun_item, null);
			} else if (imageUrls.size() == 2) {
				contview = LayoutInflater.from(context).inflate(
						R.layout.item_typetwo, null);
			} else if (imageUrls.size() == 1) {
				contview = LayoutInflater.from(context).inflate(
						R.layout.item_typeone, null);
			} else {
				contview = LayoutInflater.from(context).inflate(
						R.layout.item_wutu, null);
			}
			holder.month = (TextView) contview.findViewById(R.id.month);
			holder.year = (TextView) contview.findViewById(R.id.today);
			holder.imgurl = (ImageView) contview.findViewById(R.id.img_one);
			holder.countext = (TextView) contview
					.findViewById(R.id.tetCountext);
			holder.delete = (TextView) contview.findViewById(R.id.txtDelete);
			holder.imgTw = (ImageView) contview.findViewById(R.id.img_two);
			holder.imgThr = (ImageView) contview.findViewById(R.id.img_three);
			contview.setTag(holder);
		} else {
			holder = (ViewHolder) contview.getTag();
		}
		holder.delete.setTag(postion);
		holder.delete.setOnClickListener(deleteLisenter);
		holder.year.setText(new SimpleDateFormat("yy").format(Long
				.parseLong(list.get(postion).getDateline()) * 1000));
		holder.month.setText(new SimpleDateFormat("MM").format(Long
				.parseLong(list.get(postion).getDateline()) * 1000) + "月");
		holder.countext.setText(list.get(postion).getMessage());
		holder.delete.setText("删除");// +list.get(postion).getDelete());

		if (imageUrls == null) {
			holder.imgurl.setVisibility(View.GONE);
			holder.imgThr.setVisibility(View.GONE);
			holder.imgTw.setVisibility(View.GONE);
		} else {
			if (imageUrls.size() >= 1) {
				XPicasso.loadImage(context,  holder.imgurl,GlobalConfig.API_URL
						+ "/picture/index?id="
						+ list.get(postion).getImg().get(0));
			}
			if (imageUrls.size() >= 2) {
				XPicasso.loadImage(context, holder.imgTw, GlobalConfig.API_URL
						+ "/picture/index?id="
						+ list.get(postion).getImg().get(1));
			}
			if (imageUrls.size() >= 3) {
				XPicasso.loadImage(context, holder.imgThr, GlobalConfig.API_URL
						+ "/picture/index?id="
						+ list.get(postion).getImg().get(2));
			}
		}
		// }
		// }
		return contview;
	}

	/**
	 * 打开图片查看器
	 * 
	 * @param position
	 * @param urls2
	 */
	protected void imageBrower(int position, ArrayList<String> urls2) {
		Intent intent = new Intent(context, ImagePagerActivity.class);
		// 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls2);
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
		context.startActivity(intent);
	}

	class ViewHolder {
		private TextView year;
		private TextView month;
		private ImageView imgurl, imgTw, imgThr;
		private TextView countext;
		private TextView delete;
		private TextView num;
	}

}
